#import "DDPassViewController.h"
#import "DDRegisterInput.h"
#import "DDWebView.h"
@interface DDPassViewController ()
@property (nonatomic, strong) DDRegisterInput *input;
@property (nonatomic, strong) DDButton *saveBtn;
@property (nonatomic, strong) DDButton *serviceBtn;
@property (nonatomic, strong) DDWebView *webView;
@end
@implementation DDPassViewController
- (void)viewDidLoad {
    [super viewDidLoad];
}
- (DDRegisterInput *)input
{
    if (!_input)
    {
        _input = [[DDRegisterInput alloc] init];
        [self.ddview addSubview:_input];
        _input.c_left.equal(self.ddview).with.coffset(GAP);
        _input.c_right.equal(self.ddview).with.coffset(-GAP);
        _input.c_height.equal(TEXTFILDHEIGHT * 3);
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
            _input.c_bottom.equal(self.ddview.c_centerY).with.coffset( - GAP * 2);
        else
            _input.c_top.equal(self.ddview.bar.c_bottom).with.coffset(GAP);
    }
    return _input;
}
- (DDButton *)saveBtn
{
    if (!_saveBtn)
    {
        _saveBtn = [[DDButton alloc] init];
        _saveBtn.title = @"确认修改";
        _saveBtn.backgroundColor = helpermanger().getPara().color;
        _saveBtn.layer.cornerRadius = GAP / 2.0;
        [_saveBtn addTarget:self action:@selector(yjd_actionForSave) forControlEvents:UIControlEventTouchUpInside];
        [self.ddview addSubview:_saveBtn];
        _saveBtn.c_left.equal(self.input);
        _saveBtn.c_right.equal(self.input);
        _saveBtn.c_height.equal(BUTTONHEIGHT);
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
            _saveBtn.c_top.equal(self.ddview.c_centerY).with.coffset(GAP * 2);
        else
            _saveBtn.c_top.equal(self.input.c_bottom).with.coffset(GAP);
    }
    return _saveBtn;
}
- (DDButton *)serviceBtn
{
    if (!_serviceBtn)
    {
        _serviceBtn = [[DDButton alloc] init];
        _serviceBtn.title = @"如有问题，请联系客服QQ";
        _serviceBtn.font = FONT(UIFontWeightRegular, 15);
        [_serviceBtn setValue:helpermanger().getPara().color forKeyPath:@"_label.textColor"];
        _serviceBtn.autoTitleSize = YES;
        [_serviceBtn addTarget:self action:@selector(yjd_actionForServiceAgreement) forControlEvents:UIControlEventTouchUpInside];
        [self.ddview addSubview:_serviceBtn];
        _serviceBtn.c_bottom.equal(self.ddview).with.coffset(-GAP);
        _serviceBtn.c_centerX.equal(self.ddview);
    }
    return _serviceBtn;
}
- (DDWebView *)webView
{
    if (!_webView)
    {
        _webView = [[DDWebView alloc] init];
        [self.view addSubview:_webView];
    }
    return _webView;
}
- (void)yjd_actionForSave
{
    if ([self.input.nameTF.text length] < 1) return [CCAlert showMessage:@"请输入原密码" hidden:nil];
    else if ([self.input.passTF.text length] < 1) return [CCAlert showMessage:@"请输入新密码" hidden:nil];
    else if ([self.input.confirmTF.text length] < 1) return [CCAlert showMessage:@"请确认新密码" hidden:nil];
    else if (![self.input.nameTF.text isNumberOrAlphabet]) return [CCAlert showMessage:@"密码仅限于数字和字母" hidden:nil];
    else if (![self.input.passTF.text isNumberOrAlphabet]) return [CCAlert showMessage:@"密码仅限于数字和字母" hidden:nil];
    else if (![self.input.confirmTF.text isNumberOrAlphabet]) return [CCAlert showMessage:@"密码仅限于数字和字母" hidden:nil];
    else if (![self.input.passTF.text isEqualToString:self.input.confirmTF.text]) return [CCAlert showMessage:@"两次密码不一致" hidden:nil];
    [CCHud startHud];
    [self.request yjd_alterPWDRequest:@{@"oldpassword":self.input.nameTF.text,@"password":self.input.passTF.text}];
}

- (void)yjd_actionForBackButton
{
    [self dismissViewControllerAnimated:NO completion:^{
        UIViewController*vc = [UIViewController currentVC];
        if ([NSStringFromClass([vc class]) isEqualToString:@"DDRegisterViewController"])
        {
            [vc dismissViewControllerAnimated:NO completion:nil];
        }
    }];
}
- (void)yjd_actionForServiceAgreement
{
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"mqq://"]])
    {
        webViewManager().show(self.webView,[NSString stringWithFormat:@"mqq://im/chat?chat_type=wpa&uin=%@&version=1&src_type=web",helpermanger().getPara().qq] );
    }
    else
    {
        [CCAlert showMessage:@"未安装QQ,请先安装" title:@"注意" enter:nil];
    }
}
#pragma mark - JTWRequestDelegate
- (void)yjd_didRequestWithReceiveData:(NSDictionary *)dict error:(NSError *)error
{
    [CCHud stopHud];
    if(error)
    {
        if ((error.code != -999 || ![error.localizedDescription isEqualToString:@"cancelled"]))
            [CCAlert showMessage:[NSString stringWithFormat:@"%@",error.localizedDescription] hidden:nil];
        return;
    }
    if (!dict)
        return;
    int code = -1;
    code = [[dict valueForKey:@"code"] intValue];
    NSString *info = [NSString stringWithFormat:@"%@",[dict valueForKey:@"info"]];
    if (code == 0)
    {
        helpermanger().insertAccountDict(@{@"name":helpermanger().getPara().account,@"password":self.input.confirmTF.text});
        [CCAlert showMessage:(([info length] > 1)?info:@"修改成功") hidden:^{
            [self yjd_actionForBackButton];
        }];
    }
    else
    {
        [CCAlert showMessage:[NSString stringWithFormat:@"%@" ,info] title:@"错误" enter:nil];
    }
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.ddview.bar.title = @"修改密码";
    if ([NSStringFromClass([self.presentingViewController class]) isEqualToString:@"DDRegisterViewController"])
    {
        self.input.nameTF.text = [self.presentingViewController valueForKey:@"pass"];
    }
    else if([NSStringFromClass([self.presentingViewController class]) isEqualToString:@"DDPersonViewController"])
    {
        NSString *json = helpermanger().getPara().accountArr[0];
        NSDictionary *dict = [json dict];        self.input.nameTF.text = [dict allValues][0];
    }
    else
    {
        self.input.nameTF.placeholder = @"原密码";
    }
    self.input.passTF.placeholder = @"输入新密码";
    self.input.confirmTF.placeholder = @"确认新密码";
    self.input.nameTF.type = JTWTextFieldTypePassword;
    [self.input setupline];
    self.saveBtn.hidden = NO;
    self.serviceBtn.hidden = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
